//
//  UICGDirectionsService.m
//  MapDirectionsV3
//
//  Created by Kishikawa Katsumi on 09/12/19.
//  Copyright Kishikawa Katsumi 2009. All rights reserved.
//

#import "UICGDirectionsService.h"
#import "UICGDirectionsRequest.h"
#import "UICGDirectionsWaypoint.h"
#import "UICGPolyline.h"
#import "JSON.h"

static UICGDirectionsService *sharedDirectionsService;

@implementation UICGDirectionsService

@synthesize legs;
@synthesize delegate;
@synthesize polylines;
@synthesize isInitialized;

+ (UICGDirectionsService *)sharedDirectionsService {
	if (!sharedDirectionsService) {
		sharedDirectionsService = [[UICGDirectionsService alloc] init];
	}
	return sharedDirectionsService;
}

- (id)init {
	self = [super init];
	if (self != nil) {
		googleMapsAPI = [[UICGoogleMapsAPI alloc] init];
		googleMapsAPI.delegate = self;
	}
	return self;
}

- (void)dealloc {
	[googleMapsAPI release];
	[legs release];
	[polylines release];
	[super dealloc];
}

- (void)goolgeMapsAPIDidFinishInitialize:(UICGoogleMapsAPI *)goolgeMapsAPI {
	isInitialized = YES;
	if ([delegate respondsToSelector:@selector(directionsServiceDidFinishInitialize:)]) {
		[delegate directionsServiceDidFinishInitialize:self];
	}
}

- (void)goolgeMapsAPI:(UICGoogleMapsAPI *)goolgeMapsAPI didFailInitializeWithError:(NSError *)error {
	if ([delegate respondsToSelector:@selector(directionsService:didFailInitializeWithError:)]) {
		[delegate directionsService:self didFailInitializeWithError:error];
	}
}

- (void)goolgeMapsAPI:(UICGoogleMapsAPI *)goolgeMapsAPI didGetObject:(NSObject *)object {
	NSDictionary *dictionary = (NSDictionary *)object;
    NSArray *routes = [dictionary objectForKey:@"routes"];
	self.legs = [[routes objectAtIndex:0] objectForKey:@"legs"];
    NSMutableArray *lines = [NSMutableArray arrayWithCapacity:[legs count]];
    for (id leg in legs) {
        [lines addObject:[UICGPolyline polylineWithDictionaryRepresentation:leg]];
    }
    self.polylines = lines;
	
	if ([delegate respondsToSelector:@selector(directionsServiceDidUpdateDirections:)]) {
		[delegate directionsServiceDidUpdateDirections:self];
	}
}

- (void)goolgeMapsAPI:(UICGoogleMapsAPI *)goolgeMapsAPI didFailWithMessage:(NSString *)message {
	if ([delegate respondsToSelector:@selector(directionsService:didFailWithMessage:)]) {
		[delegate directionsService:self didFailWithMessage:message];
	}
}

- (void)routeWithStartPoint:(NSString *)startPoint endPoint:(NSString *)endPoint options:(UICGDirectionsOptions *)options {
    NSLog(@"%s", __func__);
    UICGDirectionsRequest *request = [[UICGDirectionsRequest alloc] initWithStartPoint:startPoint endPoint:endPoint];
    request.travelMode = options.travelMode;
	[googleMapsAPI stringByEvaluatingJavaScriptFromString:
	 [NSString stringWithFormat:@"route(%@)", [request JSONRepresentation]]];
}

- (void)routeFromWaypoints:(NSArray *)waypoints options:(UICGDirectionsOptions *)options {
    NSLog(@"%s", __func__);
    UICGDirectionsRequest *request = [[UICGDirectionsRequest alloc] initWithStartPoint:[waypoints objectAtIndex:0] endPoint:[waypoints lastObject]];
    request.travelMode = options.travelMode;
    NSMutableArray *a = [NSMutableArray arrayWithCapacity:[waypoints count] - 2];
    for (int i = 1; i < [waypoints count] - 1; i++) {
        UICGDirectionsWaypoint *waypoint = [[UICGDirectionsWaypoint alloc] initWithLocation:[waypoints objectAtIndex:i]];
    	[a addObject:waypoint];
        [waypoint release];
    }
    request.waypoints = a;
	[googleMapsAPI stringByEvaluatingJavaScriptFromString:
	 [NSString stringWithFormat:@"route(%@)", [request JSONRepresentation]]];
}

- (NSInteger)numberOfRoutes {
	return [legs count];
}

- (id)routeAtIndex:(NSInteger)index {
	return [legs objectAtIndex:index];
}

@end
